<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp" %>
<%
    String uuid = UUID.randomUUID().toString();
    request.setAttribute("uuid", uuid);
%>
<div class="row">
    <div class="col-md-3">
    	<div class="box box-primary">
    		<div class="box-header with-border">
    			<h3 class="box-title">
    			
    			</h3>
    			<div class="box-tools pull-right" id="systemTreeTool_${uuid}">
    				<shiro:hasPermission name="DATADIC_ITEM_ADD">
	            	<shiro:hasPermission name="SYSTEM_ADD">
	                <a href="javascript:void(0)" class="btn btn-sm btn-success addBt" title="添加系统">
	                   	<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
	                </a>
	                </shiro:hasPermission>
	                </shiro:hasPermission>
	                
	                <shiro:hasPermission name="DATADIC_ITEM_MOD">
	                <shiro:hasPermission name="SYSTEM_MOD">
	                <a href="javascript:void(0)" class="btn btn-sm btn-primary editBt" title="修改">
	                    <span class="glyphicon glyphicon-edit" aria-hidden="true"></span>
	                </a>
	                </shiro:hasPermission>
	                </shiro:hasPermission>
	                
	                <shiro:hasPermission name="DATADIC_ITEM_DEL">
	                <shiro:hasPermission name="SYSTEM_DEL">
	                <a href="javascript:void(0)" class="btn btn-sm btn-danger delBt" title="删除">
	                    <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
	                </a>
	                </shiro:hasPermission>
	                </shiro:hasPermission>
	                
	                <shiro:hasPermission name="DATADIC_ITEM_MOD">
	                <shiro:hasPermission name="SYSTEM_MOD">
	                <div class="btn-group">
					  <button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
					   	 交换 <span class="caret"></span>
					  </button>
					  <ul class="dropdown-menu">
					    <li><a href="javascript:void(0)" class="expBtn">导出</a></li>
					    <li><a href="javascript:void(0)" class="impBtn" id="impSysMenuBtn">导入</a></li>
					  </ul>
					</div>
	                </shiro:hasPermission>
	                </shiro:hasPermission>
    			</div>
    		</div>
    		<div class="box-body">
    			<div id="dataDicItemTree_${uuid}" class="ztree">
        		</div>
    		</div>
    	</div>
    </div>
    <div class="col-md-3">
    	<div class="box box-primary">
    		<div class="box-header with-border">
    			<h3 class="box-title">
    			
    			</h3>
    			<div class="box-tools pull-right" id="menuToolForm_${uuid}">
    				<shiro:hasPermission name="MENU_ADD">
	                <a href="javascript:void(0)" class="btn btn-sm btn-success addBt">
	                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 添加菜单
	                </a>
	                </shiro:hasPermission>
	                
	                <shiro:hasPermission name="MENU_DEL">
	                <a href="javascript:void(0)" class="btn btn-sm btn-danger delBt">
	                   <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>  删除
	                </a>
	                </shiro:hasPermission>
    			</div>
    		</div>
    		<div class="box-body">
    			<div id="menuTree_${uuid}" class="ztree">
            	</div>
    		</div>
    	</div>
    </div>
    <div class="col-md-6">
    	<div class="box box-primary">
    		<div class="box-body" id="menuInfoPanel_${uuid}" >
    			
    		</div>
    	</div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        var uuid = '${uuid}';
        var menuEditListener;

        var menuToolForm = $("#menuToolForm_"+uuid);
        var menuInfoPanel = $("#menuInfoPanel_"+uuid);
        var menuTree = $("#menuTree_"+uuid);

        var dataDicItemTree = $("#dataDicItemTree_"+uuid);
        var systemTreeTool = $("#systemTreeTool_"+uuid);

        var setMenuInfoPanel = function (url) {
            menuInfoPanel.html("");
            if (url) {
                menuInfoPanel.load(url);
            }

        };

        menuTree = $.fn.zTree.init(menuTree, {
            treeId: "menuTree",
            async: {
                enable: true,
                url: ctx + "/menuAction/queryMenuTreeNodes.do",
                autoParam: ["id=pid"],
                otherParam: {},
                dataFilter: function (treeId, parentNode, responseData) {
                    if (responseData) {
                        $.each(responseData, function (i, n) {
                            if (n && n.childrenSize > 0) {
                                n.isParent = true;
                            }
                        });
                    }
                    return responseData;
                }
            },
            data: {
                key: {
                    name: "text"
                }
            },
            edit: {
                enable: true,
                showRemoveBtn: false,
                showRenameBtn: false,
                drag: {
                    isCopy: false
                }
            },
            callback: {
                beforeAsync: function (treeId, treeNode) {
                    return !!menuTree.sysFlag;
                },
                onDrop: function (e, treeId, treeNodes, targetNode, moveType, isCopy) {
                    if (treeNodes && treeNodes.length > 0) {
                        var treeNode = treeNodes[0];
                        var pNode = menuTree.getNodeByTId(treeNode.parentTId);
                        var ids = [];
                        $.each(pNode.children, function (i, n) {
                            ids.push(n.id);
                        });
                        $.post(ctx + "/menuAction/seqNodes.do", {ids: ids.join(","), pid: pNode.id}, function (data) {
                            if (data.success) {
                            } else {
                            	webUtils.error(data.msg);
                            }
                        }, "json");
                    }
                },
                onClick: function (e, treeId, treeNode, clickFlag) {
                    if (clickFlag != 0) {
                        if (menuEditListener) {
                            menuEditListener.remove();
                        }
                        menuEditListener = top.webUtils.topicSubscribe("menu.edit.success", function (args) {
                            var cnode = args.obj;
                            menuTree.updateNode($.extend(treeNode, cnode));
                        });
                        setMenuInfoPanel(ctx + "/menuAction/editView.do?id=" + treeNode.id);
                    }
                }
            }
        }, [{
                text: "菜单目录",
                id: "-1",
                isParent: true,
                open: true
            }]);

        menuToolForm.find(".addBt").click(function () {
            if (!menuTree.sysFlag) {
            	webUtils.alert("请选择具体的系统");
                return false;
            }

            var pid = '-1';
            var nodes = menuTree.getSelectedNodes();
            if (nodes && nodes.length > 0) {
                var node = nodes[0];
                pid = node.id;
            }
            if (menuEditListener) {
                menuEditListener.remove();
            }
            menuEditListener = top.webUtils.topicSubscribe("menu.edit.success", function (args) {
                var cnode = args.obj;
                menuTree.addNodes(node, cnode);
                menuEditListener.remove();
                setMenuInfoPanel();
            });
            setMenuInfoPanel(ctx + "/menuAction/editView.do?pid=" + pid + "&sysFlag=" + menuTree.sysFlag);

        });

        menuToolForm.find(".delBt").click(function () {
            var nodes = menuTree.getSelectedNodes();
            if (nodes && nodes.length > 0) {
            	webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
					if(result){
						var node = nodes[0];
		                var ids = [];
		                ids.push(node.id);
		                $.post(ctx + "/menuAction/delMenu.do", {ids: ids.join(",")}, function (data) {
		                    if (data.success) {
		                        menuTree.removeNode(node);
		                    } else {
		                    	webUtils.error(data.msg);
		                    }
		                }, 'json');
					}
        		});
                

            } else {
            	webUtils.alert("请勾选至少个");
            }
        });
        
       
        
        


        dataDicItemTree = $.fn.zTree.init(dataDicItemTree, {
            treeId: "dataDicItemTree",
            async: {
                enable: true,
                url: ctx + "/dataDicAction/queryDataDicItemTreeNodes.do?dataDicCode=SYSTEM",
                autoParam: ["id=pid"],
                dataFilter: function (treeId, parentNode, responseData) {
                    if (responseData) {
                        $.each(responseData, function (i, n) {
                            if (n.childrenSize > 0) {
                                n.isParent = true;
                            }
                        });
                    }
                    return responseData;
                }
            },
            data: {
                key: {
                    name: "text"
                }
            },
            edit: {
            },
            callback: {
                onClick: function (e, treeId, treeNode, clickFlag) {
                    if (clickFlag != 0) {
                        var sysFlag = treeNode.value;
                        menuTree.sysFlag = sysFlag;
                        menuTree.setting.async.otherParam.sysFlag = sysFlag;
                        menuTree.reAsyncChildNodes(menuTree.getNodesByParam("id", "-1")[0], "refresh");
                    }
                }
            }
        }, [{
                text: "系统目录",
                id: "-1",
                isParent: true,
                open: true
            }]);
        dataDicItemTree.reAsyncChildNodes(dataDicItemTree.getNodesByParam("id", "-1")[0], "refresh");

        var editSystem = function (params) {
            params = params || {};
            var t = [];
            if (params.id) {
                t.push("id=" + params.id);
            }
            if (params.pid) {
                t.push("pid=" + params.pid);
            }
            t.push("dataDicCode=SYSTEM");
            
            var dialog=webUtils.dialog({
           	 title:'编辑系统',
           	 url:ctx + '/dataDicAction/dataDicItemEditView.do?' + t.join("&"),
           	 onremove:function () {
                    if (params.onClose) {
                        params.onClose.call(null);
                    }
                }
            });
            return dialog;
        };

        systemTreeTool.find(".addBt").click(function () {
            var nodes = dataDicItemTree.getSelectedNodes();
            var pid = '-1';
            if (nodes && nodes.length > 0) {
                var node = nodes[0];
                pid = node.id;
            }
            var sub = top.webUtils.topicSubscribe("dataDicItem.edit.success", function (args) {
                console.log(args);
            	var cnode = args.obj;
                dataDicItemTree.addNodes(node, cnode);
                dialog.close();
            });
            var dialog = editSystem({
                pid: pid,
                onClose: function () {
                    sub.remove();
                }
            });

        });

        systemTreeTool.find(".editBt").click(function () {
            var nodes = dataDicItemTree.getSelectedNodes();
            if (nodes && nodes.length > 0) {
                var node = nodes[0];
                var sub = top.webUtils.topicSubscribe("dataDicItem.edit.success", function (args) {
                    var cnode = args.obj;
                    dataDicItemTree.updateNode($.extend(node, cnode));
                    dialog.close();
                });
                var dialog = editSystem({
                    id: node.id,
                    onClose: function () {
                        sub.remove();
                    }
                });
            } else {
            	webUtils.alert("请选择其中一个");
            }
        });

        systemTreeTool.find(".delBt").click(function () {
            var nodes = dataDicItemTree.getSelectedNodes();
            if (nodes && nodes.length > 0) {
            	webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
					if(result){
						var node = nodes[0];
		                var ids = [];
		                ids.push(node.id);
		                $.post(ctx + "/dataDicAction/delDataDicItems.do", {ids: ids.join(",")}, function (data) {
		                    if (data.success) {
		                        dataDicItemTree.removeNode(node);
		                    } else {
		                        webUtils.error(data.msg);
		                    }
		                }, 'json');
					}
        		});

            } else {
            	webUtils.alert("请选择需要删除的");
            }
        });
        
        systemTreeTool.find(".expBtn").click(function(){
        	var nodes = dataDicItemTree.getSelectedNodes();
            if (nodes && nodes.length > 0) {
            	var node = nodes[0];
        		window.open(ctx + "/menuAction/export.do?sysFlag="+node.code);
            } else {
            	webUtils.alert("请选择需要导出菜单的系统");
            }
        });
        
        var uploader
        webUtils.fileUploader({
        	server:ctx+"/menuAction/import.do",
        	uploadPick:"#impSysMenuBtn",
        	duplicate:false,
        	accept: {
                title: 'json文件类型',
                //需要与platform.upload.allowFileTypes配置保持一致
                extensions: 'json'
            },
            afterInit:function(u,uder){
            	uploader=u;
            },
            beforeFileQueued:function(file){
            	var nodes = dataDicItemTree.getSelectedNodes();
                if (nodes && nodes.length > 0) {
                	return true;
                }else{
                	return false;
                }
            },
            uploadBeforeSend:function(object , data ,headers){
            	var nodes = dataDicItemTree.getSelectedNodes();
            	var node = nodes[0];
            	data.sysFlag=node.code;
            }
        });
        
        systemTreeTool.find(".impBtn").mouseenter(function(){
        	uploader.refresh();
        });
    });
</script>